package com.terren.spider.core.html.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.terren.spider.core.html.configuration.Configuration;
import com.terren.spider.core.html.configuration.DynamicType;
import com.terren.spider.core.html.configuration.SpiderSchedulerType;
import com.terren.spider.core.html.scheme.ICustomScheme;
import com.terren.spider.entity.config.SysConfig;
import com.terren.spider.entity.core.CatagoryType;
import com.terren.spider.entity.core.Entry;
import com.terren.spider.util.BasicUtil;
import com.terren.spider.util.common.BeanUtil;
import com.terren.spider.util.common.RandomUA;

import us.codecraft.webmagic.utils.UrlUtils;

public class ConfigurationUtil extends BasicUtil{
	private Configuration configuration;
	
	
	public ConfigurationUtil(Entry entry) {
		initConfig(entry);
	}


	public Configuration initConfig(Entry entry) {
		configuration = new Configuration();
		configuration.setEntry(entry);
		/**webmagic 支持自动获取字符集 所以不用设置*/
//		if (null != entry.getCharset()) {
//			configuration.setCharset(entry.getCharset());//字符集
//		}
		if (null != entry) {
			try {
				List<ICustomScheme> schemes = BeanUtil.createBeanList(SysConfig.SCHEME_PATH,
						entry.getAct());
				for (ICustomScheme scheme : schemes) {
					configuration.addScheme(scheme);
				}
			} catch (Exception e) {
				log.error("[entry:"+entry.getEntryId()+"] init schemes error:",e);
			}
			
			
			if (null!=entry.getEntryUrl()) {
				configuration.setDomain(UrlUtils.getDomain(entry.getEntryUrl()));
			}
			if (null!=entry.getDynamic()) {
				configuration.getProxyRequestParam().setUrlDynamic(DynamicType.valueOfType(entry.getDynamic()));//列表页是否动态加载
			}
			if (null!=entry.getTimeout()&&entry.getTimeout()!=0) {
				configuration.setTimeOut(entry.getTimeout()*1000);//链接超时
				configuration.getProxyRequestParam().setTimeout(entry.getTimeout()*1000);//代理端口
			}
			if (null!=entry.getCatagoryId()) {
				configuration.setCatagoryType(CatagoryType.valueOfType(entry.getCatagoryId()));//爬取类型
			}
			if (null != entry.getEntryUseProxy() && entry.getEntryUseProxy() == 1) {//使用代理，默认0 不使用
				configuration.getProxyRequestParam().setEntryUseProxy(true);
			}
			if (null != entry.getContentUseProxy() && entry.getContentUseProxy() == 1) {//使用代理，默认0 不使用
				configuration.getProxyRequestParam().setContentUseProxy(true);
			}
			
			if (null != entry.getProxyPort()&&entry.getProxyPort()!=0) {
				configuration.getProxyRequestParam().setProxyPort(entry.getProxyPort());//代理端口
			}
			if (null != entry.getProxyHost()&&!"".equals(entry.getProxyHost())) {
				configuration.getProxyRequestParam().setProxyHost(entry.getProxyHost());//代理服务器
			}
			
			if (null!=entry.getThreadNum()&&entry.getThreadNum()!=0) {
				configuration.setThreadNum(entry.getThreadNum());
			}
			if (null!=entry.getCharset()&&!"".equals(entry.getCharset())) {
				configuration.setCharset(entry.getCharset());
			}
			if (null!=entry.getCacheMode()) {
				configuration.setSchedulerType(SpiderSchedulerType.valueOfType(entry.getCacheMode()));//(QueueScheduler使用内存队列保存待抓取URL)对待抓取的URL队列进行管理,对已抓取的URL进行去重。
			}
		}
		configuration.setUserAgent(RandomUA.getRandomWebUa());// ua 设置
		configuration.setCookies(getCookie(configuration));
		configuration.setHeaders(getHeader(configuration));
		for (ICustomScheme scheme : configuration.getSchemes()) {// 自定义列表页方案。等
			if (BeanUtil.includeMethod("setRenderPage", scheme.getClass())) {
				Integer page = scheme.setRenderPage();
				if(null!=page){
					configuration.getProxyRequestParam().setPage(page);
					log.info("[entry:"+configuration.getEntry().getEntryId()+"]"+"[scheme:"+scheme.getClass().getName());
					log.info("[method:setRenderPage][page:"+page+"]");
					break;
				}
			}
		}
		for (ICustomScheme scheme : configuration.getSchemes()) {// 代理服务器是否使用阿布云（这里是指 代理服务器的阿布云配置：aby=true）
			if (BeanUtil.includeMethod("useAbuyunProxy", scheme.getClass())) {
				boolean aby = scheme.useAbuyunProxy();
				configuration.getProxyRequestParam().setUseAbuyun(aby);
				log.info("[entry:"+configuration.getEntry().getEntryId()+"]"+"[scheme:"+scheme.getClass().getName());
				log.info("[method:useAbuyunProxy][aby:"+aby+"]");
				break;
			}
		}
		for (ICustomScheme scheme : configuration.getSchemes()) {// 自定义列表页方案。等
			if (BeanUtil.includeMethod("setTimeWait", scheme.getClass())) {
				Integer timewait = scheme.setTimeWait();
				if(null!=timewait){
					configuration.getProxyRequestParam().setTimewait(timewait);
					log.info("[entry:"+configuration.getEntry().getEntryId()+"]"+"[scheme:"+scheme.getClass().getName());
					log.info("[method:setRenderPage][timewait:"+timewait+"]");
					break;
				}
			}
		}
		for (ICustomScheme scheme : configuration.getSchemes()) {// 搜索页 每页显示的条目数
			if (BeanUtil.includeMethod("setPageRecordSize", scheme.getClass())) {
				Integer pageRecordSize = scheme.setPageRecordSize();
				if(null!=pageRecordSize){
					configuration.setPageRecordSize(pageRecordSize);
					log.info("[entry:"+configuration.getEntry().getEntryId()+"]"+"[scheme:"+scheme.getClass().getName());
					log.info("[method:setPageRecordSize][pageRecordSize:"+pageRecordSize+"]");
					break;
				}
			}
		}
		
		return configuration;
	}
	
	
	public Map<String, String> getHeader(Configuration configuration){
		Map<String, String> headers = new HashMap<>();
		if (configuration.getUserAgent()!=null && !configuration.getUserAgent().equals("")) {
			headers.put("User-Agent", configuration.getUserAgent());
		}
		for (ICustomScheme scheme : configuration.getSchemes()) {
			if (BeanUtil.includeMethod("getHeader", scheme.getClass())) {
				Map<String, String> customHeaders = scheme.getHeader(configuration.getEntry());
				log.info("[entry:"+configuration.getEntry().getEntryId()+"]"+"[scheme:"+scheme.getClass().getName());
				log.info("[method:getHeader]");
				headers.putAll(customHeaders);
			}
		}
		return headers;
	}
	
	
	//  获取cookie信息
	/**
	 * 获取cookie  
	 * @param entry
	 * @return
	 */
    public Map<String, String> getCookie(Configuration configuration){
    	Map<String, String> cookies = new HashMap<>();
    	for (ICustomScheme scheme : configuration.getSchemes()) {
    		if (BeanUtil.includeMethod("getCookie", scheme.getClass())) {
    			log.info("[entry:"+configuration.getEntry().getEntryId()+"]"+"[scheme:"+scheme.getClass().getName());
    			log.info("[method:getCookie]");
        		cookies.putAll(scheme.getCookie(configuration.getEntry()));
    		}
    	}
        return cookies;
    }
	

	public Configuration getConfiguration() {
		return configuration;
	}


	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
    
}
